'''
Author: Leo.wang wanglizhigs@163.com
Date: 2025-10-31 22:07:39
LastEditors: Leo.wang wanglizhigs@163.com
LastEditTime: 2025-11-08 23:22:59
FilePath: /medical-companion-server-python/app/patient/views.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
from django.shortcuts import render

# Create your views here.
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.response import Response
from rest_framework import status
from .models import McPatient
from .serializers import McPatientSerializer
from app.common.pagination import StandardResultsSetPagination
from rest_framework.permissions import IsAuthenticated



@api_view(['GET'])
@permission_classes([IsAuthenticated])   # 只在这个接口启用 JWT 认证
def patient_list(request):

    openid = request.auth.get('openid')  # 来自 token 的 openid
    user = request.user 
    # print("jwt----------------------->", openid, user, user.id, user.phone)
    
    if getattr(request.user, "_role_type", "") == "wechat":
      items = McPatient.objects.filter(user=user).order_by('-created_time')
    elif getattr(request.user, "_role_type", "") == "manage":
      items = McPatient.objects.all().order_by('-created_time')
      
    
    # 分页
    paginator = StandardResultsSetPagination()
    result_page = paginator.paginate_queryset(items, request)
    serializer = McPatientSerializer(result_page, many=True)

    return paginator.get_paginated_response(serializer.data)

@api_view(['GET'])
@permission_classes([IsAuthenticated])  
def patient_detail(request, pk):
    try:
        item = McPatient.objects.get(pk=pk)
    except McPatient.DoesNotExist:
        return Response({'error': 'Not found'}, status=status.HTTP_404_NOT_FOUND)
    serializer = McPatientSerializer(item)
    return Response(serializer.data)

@api_view(['POST'])
@permission_classes([IsAuthenticated])  
def patient_create(request):
    serializer = McPatientSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['PUT'])
@permission_classes([IsAuthenticated])  
def patient_update(request, pk):
    try:
        item = McPatient.objects.get(pk=pk)
    except McPatient.DoesNotExist:
        return Response({'error': 'Not found'}, status=status.HTTP_404_NOT_FOUND)
    serializer = McPatientSerializer(item, data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@api_view(['DELETE'])
@permission_classes([IsAuthenticated])  
def patient_delete(request, pk):
    try:
        item = McPatient.objects.get(pk=pk)
    except McPatient.DoesNotExist:
        return Response({'error': 'Not found'}, status=status.HTTP_404_NOT_FOUND)
    item.delete()
    return Response(status=status.HTTP_204_NO_CONTENT)
